package org.springblade.common.utils;

import org.apache.commons.lang.StringUtils;
import org.springblade.common.model.ITreeModel;
import org.springblade.common.model.ITreeModelWrapper;
import org.springblade.common.model.ITreePathSupport;

import java.util.*;
import java.util.function.Function;

public class TreeModelHelper {
	public TreeModelHelper() {
	}

	public static <T extends ITreeModelWrapper> void visitTreeNodes(List<T> treeModes, Function<T, Boolean> vistor) {
		Queue<T> queue = new LinkedList(treeModes);

		ITreeModelWrapper t;
		for (HashSet visited = new HashSet(); !queue.isEmpty(); visited.add(t.getId())) {
			t = (ITreeModelWrapper) queue.poll();
			boolean result = (Boolean) vistor.apply((T) t);
			if (!result) {
				break;
			}

			if (!visited.contains(t.getId())) {
				List children = t.getChildren();
				if (children != null) {
					queue.addAll(children);
				}
			}
		}
	}
}
